package com.ys.chatserver.tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class EmptyUtils {
    /**
     * 判断对象为空
     *
     * @param obj 对象名
     * @return 是否为空
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if ((obj instanceof List)) {
            return ((List) obj).isEmpty();
        }
        if ((obj instanceof String)) {
            return ((String) obj).trim().isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }
        return false;
    }

    /**
     * 判断对象不为空
     *
     * @param obj 对象名
     * @return 是否不为空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }


    /**
     * 将用逗号分隔开的数值转为数组
     */
    public static List<String> getListObjectSplitValue(String value) {
        if (isNotEmpty(value)) {
            String[] list = value.split(",");
            return new ArrayList<>(Arrays.asList(list));
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 获取拼接字符串
     */
    public static String getStringSplitValue(List<String> list) {
        StringBuilder builder = new StringBuilder();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1) {
                    builder.append(list.get(i));
                } else {
                    builder.append(list.get(i)).append(",");
                }
            }
        }
        return builder.toString();
    }

    /**
     * 将用逗号分隔开的数值转为数组
     */
    public static List<String> getListStringSplitValue(String value) {
        if (isNotEmpty(value)) {
            String[] list = value.split(":");
            return new ArrayList<>(Arrays.asList(list));
        } else {
            return new ArrayList<>();
        }
    }
}
